home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir24 / jnos110g.zip / PPP.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  27KB  |  831 lines

  1. /*
  2.  *  PPP.C   -- Send and receive datagrams on serial lines with
  3.  *         Point-to-Point Protocol
  4.  *
  5.  *  This implementation of PPP is declared to be in the public domain.
  6.  *
  7.  *  Based (in part) upon previous implementations by:
  8.  *  1989    -- Drew Daniel Perkins      (ddp@andrew.cmu.edu)
  9.  *         Carnegie Mellon University
  10.  *  09-90   -- Katie Stevens        (dkstevens@ucdavis.edu)
  11.  *         UC Davis, Computing Services
  12.  *
  13.  *  Jan 91  Bill_Simpson@um.cc.umich.edu
  14.  *      Computer Systems Consulting Services
  15.  *
  16.  *  Feb 91  Glenn McGregor          (ghm@merit.edu)
  17.  *      Testing and suggestions.
  18.  *
  19.  *  May 91  Bill Simpson & Glenn McGregor
  20.  *      Update to newest LCP and IPCP draft RFCs.
  21.  *      Add quick installation features.
  22.  *      Add support for echo and discard message sending.
  23.  *
  24.  *  Jul 91  Glenn McGregor & Bill Simpson
  25.  *      Improve PAP user interface and fix related bugs.
  26.  *      Remove pwaits and "phase machine".
  27.  */
  28.   
  29. #include "global.h"
  30. #ifdef PPP
  31. #include "mbuf.h"
  32. #include "proc.h"
  33. #include "iface.h"
  34. #include "internet.h"
  35. #include "ip.h"
  36. #include "slhc.h"
  37. #ifdef LINUX
  38. #include "lxasy.h"
  39. #else
  40. #include "i8250.h"
  41. #endif
  42. #include "asy.h"
  43. #include "pktdrvr.h"
  44. #include "socket.h"
  45. #include "devparam.h"
  46. #include "ppp.h"
  47. #include "pppfsm.h"
  48. #include "ppplcp.h"
  49. #include "ppppap.h"
  50. #include "pppipcp.h"
  51. #include "trace.h"
  52.   
  53. /* Routines local to this file */
  54. static struct mbuf *htonppp __ARGS((struct ppp_hdr *ppp, struct mbuf *data));
  55.   
  56. static void ppp_log __ARGS((struct ppp_s *ppp_p, char *comment));
  57. static void ppp_error __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  58. static void ppp_skipped __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  59.   
  60. static int ppp_raw __ARGS((struct iface *ifp, struct mbuf *data));
  61.   
  62. static void ppp_recv __ARGS((int dev, void *p1, void *p2));
  63.   
  64. static int ppp_iostatus __ARGS((struct iface *ifp, int command, int32 value));
  65. static int ppp_discard  __ARGS((struct iface *ifp, struct mbuf *bp));
  66. static int ppp_echo __ARGS((struct iface *ifp, struct mbuf *bp));
  67.   
  68.   
  69. /*
  70.  * FCS lookup table as generated by fcsgen.c
  71.  */
  72. int16 fcstab[256] = {
  73.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  74.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  75.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  76.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  77.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  78.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  79.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  80.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  81.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  82.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  83.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  84.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  85.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  86.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  87.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  88.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  89.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  90.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  91.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  92.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  93.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  94.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  95.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  96.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  97.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  98.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  99.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  100.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  101.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  102.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  103.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  104.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  105. };
  106.   
  107. #define pppfcs(fcs, c)      ((fcs >> 8) ^ fcstab[(fcs ^ c) & 0x00ff])
  108. #define SP_CHAR         0x20
  109.   
  110.   
  111. /****************************************************************************/
  112.   
  113. /* Convert PPP header in host form to network form */
  114. static struct mbuf *
  115. htonppp(ppp, data)
  116. struct ppp_hdr *ppp;
  117. struct mbuf *data;
  118. {
  119.     struct mbuf *bp;
  120.     register char *cp;
  121.   
  122.     /* Prepend header onto packet data */
  123.     if ((bp = pushdown(data, PPP_HDR_LEN)) == NULLBUF) {
  124.         free_p( data );
  125.         return NULLBUF;
  126.     }
  127.   
  128.     /* Load header with proper values */
  129.     cp = bp->data;
  130.     *cp++ = ppp->addr;
  131.     *cp++ = ppp->control;
  132.     cp = put16(cp,ppp->protocol);
  133.   
  134.     return bp;
  135. }
  136.   
  137.   
  138. /************************************************************************/
  139. /* General log routine */
  140. static void
  141. ppp_log( ppp_p, comment )
  142. struct ppp_s *ppp_p;
  143. char *comment;
  144. {
  145.     if (ppp_p->trace)
  146.         trace_log(ppp_p->iface,"%s PPP %s",
  147.         ppp_p->iface->name,
  148.         comment);
  149. }
  150.   
  151.   
  152. /* Bad packet */
  153. static void
  154. ppp_error(ppp_p, bp, comment)
  155. struct ppp_s *ppp_p;
  156. struct mbuf *bp;
  157. char *comment;
  158. {
  159.     free_p(bp);
  160.     ppp_log( ppp_p, comment );
  161. }
  162.   
  163.   
  164. /* Unknown type input packet */
  165. static void
  166. ppp_skipped(ppp_p, bp, comment)
  167. struct ppp_s *ppp_p;
  168. struct mbuf *bp;
  169. char *comment;
  170. {
  171.     struct ipcp_s *ipcp_p;
  172.   
  173.     if ( (ipcp_p = ppp_p->fsm[IPcp].pdv) != NULL ) {
  174.         slhc_toss( ipcp_p->slhcp );
  175.     }
  176.     ppp_error( ppp_p, bp, comment );
  177. }
  178.   
  179.   
  180. /****************************************************************************/
  181. /* Send IP datagram with Point-to-Point Protocol */
  182. int
  183. ppp_send(bp,ifp,gateway,prec,del,tput,rel)
  184. struct mbuf *bp;    /* Buffer to send */
  185. struct iface *ifp;  /* Pointer to interface control block */
  186. int32 gateway;      /* Ignored (PPP is point-to-point) */
  187. int prec;
  188. int del;
  189. int tput;
  190. int rel;
  191. {
  192.     struct ppp_s *ppp_p;
  193.     struct ipcp_s *ipcp_p;
  194.     int protocol = PPP_IP_PROTOCOL;
  195.   
  196.     if (ifp == NULLIF
  197.     || (ppp_p = ifp->edv) == NULL) {
  198.         free_p(bp);
  199.         return -1;
  200.     }
  201.   
  202.     if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  203.         ppp_error( ppp_p, bp, "not open for IP traffic" );
  204.         ppp_p->OutError++;
  205.         return -1;
  206.     }
  207.   
  208.     ipcp_p = ppp_p->fsm[IPcp].pdv;
  209.     if (ipcp_p->remote.work.negotiate & IPCP_N_COMPRESS) {
  210.         /* Attempt IP/TCP header compression */
  211.         switch ( slhc_compress(ipcp_p->slhcp, &bp,
  212.         ipcp_p->remote.work.slot_compress) ) {
  213.             case SL_TYPE_IP:
  214.                 protocol = PPP_IP_PROTOCOL;
  215.                 break;
  216.             case SL_TYPE_COMPRESSED_TCP:
  217.                 protocol = PPP_COMPR_PROTOCOL;
  218.                 break;
  219.             case SL_TYPE_UNCOMPRESSED_TCP:
  220.                 protocol = PPP_UNCOMP_PROTOCOL;
  221.                 break;
  222.             default:
  223.                 ppp_error( ppp_p, bp, "bad IP packet" );
  224.                 ppp_p->OutError++;
  225.                 return -1;
  226.         };
  227.     }
  228.     return (*ifp->output)(ifp,NULLCHAR,NULLCHAR,protocol,bp);
  229. }
  230.   
  231.   
  232. /* Send a packet with PPP header */
  233. int
  234. ppp_output(ifp,dest,source,protocol,data)
  235. struct iface *ifp;  /* Pointer to interface control block */
  236. char *dest;     /* Dest addr (ignored; PPP is point-to-point) */
  237. char *source;       /* Source addr (ignored; PPP is point-to-point) */
  238. int16 protocol;     /* PPP Protocol Type field */
  239. struct mbuf *data;  /* Actual data to be sent */
  240. {
  241.     struct ppp_s *ppp_p;
  242.     struct mbuf *bp;
  243.     struct ppp_hdr hdr;
  244.   
  245.     if (ifp == NULLIF
  246.     || (ppp_p = ifp->edv) == NULL) {
  247.         free_p(data);
  248.         return -1;
  249.     }
  250.   
  251.     if (ppp_p->phase == pppDEAD) {
  252.         ppp_error( ppp_p, data, "line not up" );
  253.         ppp_p->OutError++;
  254.         return -1;
  255.     }
  256.   
  257.     hdr.addr = HDLC_ALL_ADDR;
  258.     hdr.control = HDLC_UI;
  259.     hdr.protocol = protocol;
  260.   
  261.     if ((bp = htonppp(&hdr, data)) == NULLBUF) {
  262.         ppp_log( ppp_p, Nospace );
  263.         ppp_p->OutMemory++;
  264.         return -1;
  265.     }
  266.   
  267.     return (*ifp->raw)(ifp,bp);
  268. }
  269.   
  270.   
  271. /* Encode a raw packet in PPP framing, put on link output queue */
  272. static int
  273. ppp_raw(ifp,bp)
  274. struct iface *ifp;
  275. struct mbuf *bp;
  276. {
  277.     struct ppp_s *ppp_p = ifp->edv;
  278.     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  279.     int full_lcp, full_ac, full_p;
  280.     int16 calc_fcs = HDLC_FCS_START;
  281.     int32 accm = LCP_ACCM_DEFAULT;
  282.     struct ppp_hdr ph;
  283.     int len = PPP_HDR_LEN;
  284.     struct mbuf *vbp;
  285.     register char *cp;
  286.     register int c;
  287.   
  288.     dump(ifp,IF_TRACE_OUT,CL_PPP,bp);
  289.     ppp_p->OutTxOctetCount += len_p(bp) + 2;  /* count FCS bytes */
  290.     ifp->rawsndcnt++;
  291.     ifp->lastsent = secclock();
  292.   
  293.     /* Get the HDLC/PPP header, without actually pulling mbuf up */
  294.     if ( bp == NULLBUF || bp->cnt < PPP_HDR_LEN ) {
  295.         ppp_error( ppp_p, bp, "link header missing" );
  296.         ppp_p->OutError++;
  297.         return -1;
  298.     }
  299.   
  300.     ph.addr = bp->data[0];
  301.     ph.control = bp->data[1];
  302.     ph.protocol = get16(&bp->data[2]);
  303.   
  304.     if ( ( full_lcp = (ph.protocol == PPP_LCP_PROTOCOL) ) == 0
  305.     && (lcp_p->remote.work.negotiate & LCP_N_ACCM) ) {
  306.         accm = lcp_p->remote.work.accm;
  307.     }
  308.   
  309.     if ( ( full_p = ( full_lcp
  310.         ||  !(lcp_p->remote.work.negotiate & LCP_N_PFC)
  311.         ||  ph.protocol >= 0x00ff ) )
  312.     == 0 ) {
  313.         --len;
  314.     }
  315.   
  316.     /* Discard HDLC address and control fields if possible */
  317.     if ( ( full_ac = ( full_lcp
  318.         ||  !(lcp_p->remote.work.negotiate & LCP_N_ACFC) ) )
  319.     == 0 ) {
  320.         len -= 2;
  321.     }
  322.   
  323.     /* Load header with proper values */
  324.     bp->cnt -= PPP_HDR_LEN - len;
  325.     cp = (bp->data += PPP_HDR_LEN - len);
  326.   
  327.     if ( full_ac ) {
  328.         *cp++ = ph.addr;
  329.         *cp++ = ph.control;
  330.     }
  331.     if ( full_p )
  332.         *cp++ = (ph.protocol >> 8);
  333.     *cp++ = (ph.protocol & 0x00ff);
  334.   
  335.     /* Allocate output mbuf that's twice as long as the packet.
  336.      * This is a worst-case guess (consider a packet full of HDLC_FLAGs!)
  337.      */
  338.     if ((vbp = alloc_mbuf((int16)(2*len_p(bp) + HDLC_ENVLEN))) == NULLBUF) {
  339.         ppp_error( ppp_p, bp, Nospace );
  340.         ppp_p->OutMemory++;
  341.         return -1;
  342.     }
  343.     cp = vbp->data;
  344.   
  345.     /* No need to send an opening flag if the previous packet is still
  346.      * being transmitted.
  347.      */
  348. #ifdef LINUX
  349.     if (Asy[ifp->dev].sndq == 0) {
  350. #else
  351.         if ( Asy[ifp->dev].dma.flags == 0 ) {
  352. #endif
  353.         /* Flush out any line garbage */
  354.             *cp++ = HDLC_FLAG;
  355.             ppp_p->OutOpenFlag++;
  356.         }
  357.   
  358.     /* Copy input to output, escaping special characters */
  359.         while ((c = PULLCHAR(&bp)) != -1) {
  360.         /* Fold char value into FCS calculated so far */
  361.             calc_fcs = pppfcs(calc_fcs, c);
  362.   
  363.             if ( ((c < SP_CHAR) && (accm & (1L << c)))
  364.                 || (c == HDLC_ESC_ASYNC)
  365.             || (c == HDLC_FLAG)) {
  366.                 *cp++ = HDLC_ESC_ASYNC;
  367.                 *cp++ = (c ^ HDLC_ESC_COMPL);
  368.             } else {
  369.                 *cp++ = c;
  370.             }
  371.         }
  372.   
  373.     /* Final FCS calculation */
  374.         calc_fcs ^= 0xffff;
  375.         c = (calc_fcs & 0x00ff);    /* Least significant byte first */
  376.         if ( ((c < SP_CHAR) && (accm & (1L << c)))
  377.             ||(c == HDLC_ESC_ASYNC)
  378.         ||(c == HDLC_FLAG)) {
  379.             *cp++ = HDLC_ESC_ASYNC;
  380.             *cp++ = (c ^ HDLC_ESC_COMPL);
  381.         } else {
  382.             *cp++ = c;
  383.         }
  384.         c = (calc_fcs >> 8);        /* Most significant byte next */
  385.         if ( ((c < SP_CHAR) && (accm & (1L << c)))
  386.             ||(c == HDLC_ESC_ASYNC)
  387.         ||(c == HDLC_FLAG)) {
  388.             *cp++ = HDLC_ESC_ASYNC;
  389.             *cp++ = (c ^ HDLC_ESC_COMPL);
  390.         } else {
  391.             *cp++ = c;
  392.         }
  393.   
  394.     /* Tie off the packet */
  395.         *cp++ = HDLC_FLAG;
  396.         vbp->cnt = cp - vbp->data;
  397.   
  398.         if (ifp->trace & IF_TRACE_RAW)
  399.             raw_dump(ifp,IF_TRACE_OUT,vbp);
  400.         return asy_send(ifp->dev,vbp);
  401.     }
  402.   
  403.   
  404. /****************************************************************************/
  405. /* Packetize PPP input from device */
  406. /* (process started by ppp_init) */
  407.     void
  408.     ppp_recv(dev,p1,p2)
  409.     int dev;
  410.     void *p1;
  411.     void *p2;
  412.     {
  413.         struct iface *ifp = p1;
  414.         struct ppp_s *ppp_p = ifp->edv;
  415.         int32 accm = LCP_ACCM_DEFAULT;
  416.         int16 calc_fcs = HDLC_FCS_START;
  417.         struct mbuf *raw_bp = NULLBUF;
  418.         struct mbuf *head_bp = NULLBUF;
  419.         register struct mbuf *tail_bp = NULLBUF;
  420.         char *cp;           /* next byte in tail mbuf */
  421.         register int mode = FALSE;
  422.         register int c;
  423.   
  424.         while ( (c = get_asy(dev)) != -1 ) {
  425. #ifdef PPP_DEBUG_RAW
  426.             if (ifp->trace & IF_TRACE_RAW) {
  427.                 if ( raw_bp != NULLBUF
  428.                 || (raw_bp = alloc_mbuf( LCP_MRU_HI * 2 )) != NULLBUF ) {
  429.                     *raw_bp->data++ = c;
  430.                     raw_bp->cnt++;
  431.                     if ( raw_bp->cnt != 1 && c == HDLC_FLAG ) {
  432.                         raw_bp->data = (char *)(raw_bp + 1);
  433.                         raw_dump( ifp, IF_TRACE_IN, raw_bp );
  434.                         raw_bp->cnt = 0;
  435.                     }
  436.                 }
  437.             }
  438. #endif
  439.             if ( c == HDLC_FLAG ) {
  440.                 if ( mode & PPP_ESCAPED ) {
  441.                     ppp_skipped( ppp_p, head_bp,
  442.                     "deliberate cancellation" );
  443.                     ppp_p->InFrame++;
  444.                 } else if ( mode & PPP_TOSS ) {
  445.                     free_p( head_bp );
  446.                 } else if ( head_bp != NULLBUF ) {
  447.                     if ( calc_fcs != HDLC_FCS_FINAL ) {
  448.                         ppp_skipped( ppp_p, head_bp,
  449.                         "checksum error" );
  450.                         ppp_p->InChecksum++;
  451.                     } else {
  452.                     /* trim off FCS bytes */
  453.                         trim_mbuf(&head_bp, len_p(head_bp)-2);
  454.   
  455.                         net_route( ifp, CL_PPP, head_bp );
  456.                     }
  457.                 } else {
  458.                     ppp_p->InOpenFlag++;
  459.                 }
  460.   
  461.             /* setup for next buffer */
  462.                 mode = FALSE;
  463.                 head_bp = tail_bp = NULLBUF;
  464.                 calc_fcs = HDLC_FCS_START;
  465.                 accm = LCP_ACCM_DEFAULT;
  466.   
  467.             /* Use negotiated values if LCP finished */
  468.                 if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  469.                     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  470.   
  471.                     if (lcp_p->local.work.negotiate & LCP_N_ACCM) {
  472.                         accm = lcp_p->local.work.accm;
  473.                     }
  474.                 }
  475. #ifdef PPP_DEBUG_RAW
  476.                 if (!(ifp->trace & IF_TRACE_RAW)) {
  477.                     if ( raw_bp != NULLBUF ) {
  478.                         free_p( raw_bp );
  479.                         raw_bp = NULLBUF;
  480.                     }
  481.                 }
  482. #endif
  483.                 continue;
  484.             }
  485.   
  486.         /* We reach here for every byte inside a frame.
  487.          * (The order of the following tests is important.)
  488.          * Discard spurious control characters.
  489.          * Check for escape sequence.
  490.          * (Allow escaped escape.)
  491.          */
  492.             if ( c < SP_CHAR && (accm & (1L << c)) ) {
  493.                 continue;
  494.             } else if ( mode & PPP_ESCAPED ) {
  495.                 mode &= ~PPP_ESCAPED;
  496.                 c ^= HDLC_ESC_COMPL;
  497.             } else if ( c == HDLC_ESC_ASYNC ) {
  498.                 mode |= PPP_ESCAPED;
  499.                 continue;
  500.             }
  501.   
  502.         /* We reach here with a byte for the buffer.
  503.          * Make sure there is room for it.
  504.          */
  505.             if ( tail_bp == NULLBUF ) {
  506.                 if ((tail_bp = alloc_mbuf(PPP_ALLOC)) == NULLBUF) {
  507.                     ppp_skipped( ppp_p, tail_bp, Nospace );
  508.                     ppp_p->InMemory++;
  509.                     mode |= PPP_TOSS;
  510.                     continue;
  511.                 }
  512.                 head_bp = tail_bp;
  513.                 cp = tail_bp->data;
  514.             } else if ( tail_bp->cnt >= tail_bp->size ) {
  515.             /* Current mbuf is full */
  516.                 if ( (tail_bp->next = alloc_mbuf(PPP_ALLOC)) == NULLBUF ) {
  517.                 /* No memory, drop the whole packet */
  518.                     ppp_skipped( ppp_p, head_bp, Nospace );
  519.                     ppp_p->InMemory++;
  520.                     head_bp = NULLBUF;
  521.                     mode |= PPP_TOSS;
  522.                     continue;
  523.                 }
  524.                 tail_bp = tail_bp->next;
  525.                 cp = tail_bp->data;
  526.             }
  527.   
  528.         /* Store the byte, increment counts */
  529.             *cp++ = c;
  530.             tail_bp->cnt++;
  531.             calc_fcs = pppfcs(calc_fcs, c);
  532.         }
  533.   
  534.     /* clean up afterward */
  535.         free_p(raw_bp);
  536.         free_p(head_bp);
  537.         ifp->rxproc = NULLPROC;
  538.     }
  539.   
  540. #ifdef TURBOC_SWITCH_BUG
  541. #pragma option -G-
  542. #endif
  543.   
  544. /* Process incoming PPP packets */
  545. /* (called from network task) */
  546.     void
  547.     ppp_proc(ifp,bp)
  548.     struct iface *ifp;
  549.     struct mbuf *bp;
  550.     {
  551.         struct ppp_s *ppp_p;
  552.         struct ipcp_s *ipcp_p;
  553.         struct ppp_hdr ph;
  554.         struct mbuf *hbp;
  555.         int16 negotiated = FALSE;
  556.   
  557.         if ( ifp == NULLIF ) {
  558.             log(-1, "ppp_proc: missing iface" );
  559.             return;
  560.         }
  561.         if ( bp == NULLBUF ) {
  562.             trace_log(ifp, "ppp_proc: missing buffer" );
  563.             return;
  564.         }
  565.   
  566.         ppp_p = ifp->edv;
  567.         ppp_p->InRxOctetCount += len_p(bp) + 2;   /* count FCS bytes */
  568.   
  569.     /* Use negotiated values if LCP finished */
  570.         if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  571.             struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  572.   
  573.             negotiated = lcp_p->local.work.negotiate;
  574.         }
  575.   
  576.     /* HDLC address and control fields may be compressed out */
  577.         if ((byte_t)bp->data[0] != HDLC_ALL_ADDR) {
  578.             if (!(negotiated & LCP_N_ACFC)) {
  579.                 ppp_skipped( ppp_p, bp, "missing ALL address" );
  580.                 ppp_p->InFrame++;
  581.                 return;
  582.             }
  583.         } else if ((byte_t)bp->data[1] != HDLC_UI) {
  584.             if (!(negotiated & LCP_N_ACFC)
  585.             || !(negotiated & LCP_N_PFC)) {
  586.                 ppp_skipped( ppp_p, bp, "missing UI" );
  587.                 ppp_p->InFrame++;
  588.                 return;
  589.             }
  590.         } else {
  591.         /* skip address/control fields */
  592.             pull16(&bp);
  593.         }
  594.   
  595.     /* Initialize the expected header */
  596.         ph.addr = HDLC_ALL_ADDR;
  597.         ph.control = HDLC_UI;
  598.         ph.protocol = PULLCHAR(&bp);
  599.   
  600.     /* First byte of PPP protocol field may be compressed out */
  601.         if ( ph.protocol & 0x01 ) {
  602.             if (!(negotiated & LCP_N_PFC)) {
  603.                 ppp_skipped( ppp_p, bp, "missing upper protocol byte" );
  604.                 ppp_p->InFrame++;
  605.                 return;
  606.             }
  607.         } else {
  608.             ph.protocol = (ph.protocol << 8) | PULLCHAR(&bp);
  609.   
  610.         /* Second byte of PPP protocol field must be odd */
  611.             if ( !(ph.protocol & 0x01) ) {
  612.                 ppp_skipped( ppp_p, bp, "missing lower protocol byte" );
  613.                 ppp_p->InFrame++;
  614.                 return;
  615.             }
  616.         }
  617.   
  618.   
  619.         switch(ph.protocol) {
  620.             case PPP_IP_PROTOCOL:   /* Regular IP */
  621.                 if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  622.                     ppp_error( ppp_p, bp, "not open for IP traffic" );
  623.                     ppp_p->InError++;
  624.                     break;
  625.                 }
  626.                 ip_route(ifp,bp,0);
  627.                 break;
  628.   
  629.             case PPP_COMPR_PROTOCOL:    /* Van Jacobson Compressed TCP/IP */
  630.                 if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  631.                     ppp_skipped( ppp_p, bp, "not open for Compressed TCP/IP traffic" );
  632.                     ppp_p->InError++;
  633.                     break;
  634.                 }
  635.   
  636.                 ipcp_p = ppp_p->fsm[IPcp].pdv;
  637.                 if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  638.                     ppp_skipped( ppp_p, bp, "Compressed TCP/IP not enabled" );
  639.                     ppp_p->InError++;
  640.                     break;
  641.                 }
  642.   
  643.                 if ( slhc_uncompress(ipcp_p->slhcp, &bp) <= 0 ) {
  644.                     ppp_error( ppp_p, bp, "Compressed TCP/IP packet error" );
  645.                     ppp_p->InError++;
  646.                     break;
  647.                 }
  648.                 ip_route(ifp,bp,0);
  649.                 break;
  650.   
  651.             case PPP_UNCOMP_PROTOCOL:   /* Van Jacobson Uncompressed TCP/IP */
  652.                 if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  653.                     ppp_skipped( ppp_p, bp, "not open for Uncompressed TCP/IP traffic" );
  654.                     ppp_p->InError++;
  655.                     break;
  656.                 }
  657.   
  658.                 ipcp_p = ppp_p->fsm[IPcp].pdv;
  659.                 if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  660.                     ppp_skipped( ppp_p, bp, "Uncompressed TCP/IP not enabled" );
  661.                     ppp_p->InError++;
  662.                     break;
  663.                 }
  664.   
  665.                 if ( slhc_remember(ipcp_p->slhcp, &bp) <= 0 ) {
  666.                     ppp_error( ppp_p, bp, "Uncompressed TCP/IP packet error" );
  667.                     ppp_p->InError++;
  668.                     break;
  669.                 }
  670.                 ip_route(ifp,bp,0);
  671.                 break;
  672.   
  673.             case PPP_LCP_PROTOCOL:  /* Link Control Protocol */
  674.                 ppp_p->InNCP[Lcp]++;
  675.                 fsm_proc(&(ppp_p->fsm[Lcp]),bp);
  676.                 break;
  677.   
  678.             case PPP_PAP_PROTOCOL:  /* Password Authenticate Protocol */
  679.                 if (ppp_p->phase != pppAP
  680.                 && ppp_p->phase != pppREADY) {
  681.                     ppp_error( ppp_p, bp, "not ready for Authentication" );
  682.                     ppp_p->InError++;
  683.                     break;
  684.                 }
  685.                 ppp_p->InNCP[Pap]++;
  686.                 pap_proc(&(ppp_p->fsm[Pap]),bp);
  687.                 break;
  688.   
  689.             case PPP_IPCP_PROTOCOL: /* IP Control Protocol */
  690.                 if (ppp_p->phase != pppREADY) {
  691.                     ppp_error( ppp_p, bp, "not ready for IPCP traffic" );
  692.                     ppp_p->InError++;
  693.                     break;
  694.                 }
  695.                 ppp_p->InNCP[IPcp]++;
  696.                 fsm_proc(&(ppp_p->fsm[IPcp]),bp);
  697.                 break;
  698.   
  699.             default:
  700.                 if ( ppp_p->trace )
  701.                     trace_log(ppp_p->iface, "%s PPP Unknown packet protocol: %x;",
  702.                     ppp_p->iface->name,
  703.                     ph.protocol);
  704.                 ppp_p->InUnknown++;
  705.   
  706.         /* Build Protocol Reject packet:
  707.          * put the header back on ...
  708.          */
  709.                 if ((hbp = htonppp(&ph,bp)) == NULLBUF){
  710.                     ppp_log( ppp_p, Nospace );
  711.                     ppp_p->InMemory++;
  712.                     return;
  713.                 }
  714.         /* ... then pull off the address and control fields ... */
  715.                 pull16(&hbp);
  716.         /* ... and send it as an LCP packet */
  717.                 fsm_send( &(ppp_p->fsm[Lcp]), PROT_REJ, 0, hbp );
  718.                 break;
  719.         };
  720.     }
  721.   
  722. #ifdef TURBOC_SWITCH_BUG
  723. #pragma option -G
  724. #endif
  725.   
  726. /************************************************************************/
  727.   
  728. /* Keep track of changes in I-O status */
  729. /* (called through iface iostatus vector) */
  730.     static int
  731.     ppp_iostatus( ifp, command, value )
  732.     struct iface *ifp;
  733.     int command;
  734.     int32 value;
  735.     {
  736.         struct ppp_s *ppp_p = ifp->edv;
  737.   
  738.         switch ( command ) {
  739.             case PARAM_UP:
  740.                 ppp_log( ppp_p, "Physical layer up" );
  741.   
  742.                 if ( ppp_p->phase == pppDEAD ) {
  743.                     ppp_p->phase = pppLCP;
  744.                 }
  745.                 fsm_start( &(ppp_p->fsm[Lcp]) );
  746.                 return 0;
  747.   
  748.             case PARAM_DOWN:
  749.                 ppp_log( ppp_p, "Physical layer down" );
  750.   
  751.                 fsm_down( &(ppp_p->fsm[Lcp]) );
  752.                 ppp_p->phase = pppDEAD;
  753.                 return 0;
  754.         };
  755.         return -1;
  756.     }
  757.   
  758.   
  759.     static int
  760.     ppp_discard(ifp,bp)
  761.     struct iface *ifp;
  762.     struct mbuf *bp;
  763.     {
  764.         struct ppp_s *ppp_p = ifp->edv;
  765.   
  766.         return fsm_send(&(ppp_p->fsm[Lcp]), DISCARD_REQ, 0, bp);
  767.     }
  768.   
  769.   
  770.     static int
  771.     ppp_echo(ifp,bp)
  772.     struct iface *ifp;
  773.     struct mbuf *bp;
  774.     {
  775.         struct ppp_s *ppp_p = ifp->edv;
  776.   
  777.         return fsm_send(&(ppp_p->fsm[Lcp]), ECHO_REQ, 0, bp);
  778.     }
  779.   
  780.   
  781. /****************************************************************************/
  782. /* Initialize PPP control structures for a Point-to-Point interface */
  783.     int
  784.     ppp_init(ifp)
  785.     struct iface *ifp;
  786.     {
  787.         struct ppp_s *ppp_p;
  788.         char *ifn;
  789.   
  790.         ppp_p = callocw(1,sizeof(struct ppp_s));
  791.   
  792.         ifp->edv = ppp_p;
  793.         ifp->iostatus = ppp_iostatus;
  794.         ifp->raw = ppp_raw;
  795.         ifp->show = ppp_show;
  796.         ifp->echo = ppp_echo;
  797.         ifp->discard = ppp_discard;
  798.   
  799.         ppp_p->iface = ifp;
  800.         ppp_p->phase = pppDEAD;
  801.   
  802.         lcp_init(ppp_p);
  803.         pap_init(ppp_p);
  804.         ipcp_init(ppp_p);
  805.   
  806.         ifp->rxproc = newproc( ifn = if_name( ifp, " receive" ),
  807.         320, ppp_recv, ifp->dev, ifp, NULL, 0);
  808.         free(ifn);
  809.         return 0;
  810.     }
  811.   
  812.   
  813.     int
  814.     ppp_free(ifp)
  815.     struct iface *ifp;
  816.     {
  817.         struct ppp_s *ppp_p = ifp->edv;
  818.         int fsmi;
  819.   
  820.         alert( ifp->rxproc, -1 );
  821.   
  822.         for ( fsmi = Lcp; fsmi < fsmi_Size; ) {
  823.             fsm_free( &(ppp_p->fsm[fsmi++]) );
  824.         }
  825.         free( ppp_p->peername );
  826.         free( ppp_p );
  827.         return 0;
  828.     }
  829. #endif /* PPP */
  830.   
  831.